package stu.aistar.controller;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundListOperations;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import stu.aistar.model.pojo.Movie;
import stu.aistar.model.pojo.MovieType;
import stu.aistar.model.vo.DataJsonVo;
import stu.aistar.model.vo.Result;
import stu.aistar.service.MovieAllTypesService;
import stu.aistar.service.MovieService;
import stu.aistar.service.MovieTypeService;
import stu.aistar.util.FileUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/movies")
public class MovieController {

    @Autowired
    private MovieService movieService;

    @Autowired
    private MovieAllTypesService movieAllTypesService;

    @Autowired
    private MovieTypeService movieTypeService;

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("/all")
    @ResponseBody
    public Map<String,Object> findAll(HttpServletRequest request){
        String page = request.getParameter("page");
        String pageSize = request.getParameter("limit");
        String params = request.getParameter("searchParams");
        String name = null;
        String actor= null;
        Movie movie = null;
        //先转换成JSONObject类型
        if (null != params){
            movie = new Movie();
            JSONObject obj = JSONObject.parseObject(params);
            name= obj.getString("name");
            actor = obj.getString("actor");
            movie.setName(name);
            movie.setActor(actor);
        }
        Map<String,Object> movieMap = new HashMap<>();
        PageInfo<Movie> movies = movieService.findMoviesByPage(page,pageSize,movie);
        movieMap.put("code",0);
        movieMap.put("msg","查找成功");
        //数据总数
        movieMap.put("count",movies.getTotal());
        movieMap.put("data",movies.getList());

        //电影的类型

        ListOperations<String, MovieType> listOperations=redisTemplate.opsForList();
        BoundListOperations blo = redisTemplate.boundListOps("movieTypes");
        List<MovieType> types;
        if(null == blo || blo.size() == 0){
            types = movieTypeService.findAll();
            for (MovieType type:types) {
                listOperations.leftPush("movieTypes",type);
            }
        }else {
            types = listOperations.range("movieTypes",0,-1);

        }
        request.getSession().setAttribute("types",types);
        return movieMap;
    }

    @PostMapping("/delMovie")
    @ResponseBody
    public Result delMovie(@RequestParam(value = "ids[]") List<Integer> ids){
        Result result = null;
        System.out.println(ids);
        Integer res = movieService.delMoviesByIds(ids);
        if (res>=1){
            result = new Result("200","下架成功",null);
        }else {
            result = new Result("500","下架失败",null);
        }
        return result;
    }

    /**
     * 上映电影
     * @param ids
     * @return
     */
    @PostMapping("/showMovie")
    @ResponseBody
    public Result showMovie(@RequestParam(value = "ids[]")List<Integer> ids){
        Result result = null;
        Integer res = movieService.showMoviesByIds(ids);
        if (res>=1){
            result = new Result("200","上映成功",null);
        }else {
            result = new Result("500","上映失败",null);
        }
        return result;
    }

    /**
     * 上传电影图片
     * @param file
     * @return
     */
    @PostMapping("/uploadFile")
    @ResponseBody
    public DataJsonVo projectPictureUpload(@RequestParam("file")MultipartFile file){
        System.out.println("============================="+file);
        String server_img = FileUtils.upload(file);
        DataJsonVo vo = new DataJsonVo();
        if (server_img!=null){
            vo.setCode(200);
            vo.setMsg("上传成功！");
            HashMap<String,String> map = new HashMap<>();
            map.put("src",server_img);
            vo.setData(map);
        }else {
            vo.setCode(500);
            vo.setMsg("电影上传失败");
        }
        return vo;
    }

    @PostMapping("/save")
    @ResponseBody
    public Result saveMovie(Movie movie){
        Result result;
        Integer row = movieService.saveMovie(movie);
        String types = "";
        List<String> movieTypes = movie.getMovieTypes();
        for (int i = 0; i < movieTypes.size(); i++) {
            types += movie.getMovieTypes().get(i)+",";
        }
        Integer row2 = movieAllTypesService.saveMovieTypes(types.substring(0,types.length()-1), movie.getId());
        if(row == 1 &&row2==1){
            result = new Result("200","添加成功",null);
        }else {
            result = new Result("500","添加失败",null);
        }
        return result;
    }

    /**
     * 判断电影是否存在
     * @param name
     * @return
     */
    @PostMapping("/isExists")
    @ResponseBody
    public Result findIsExists(String name){
        Result result;
        Movie movie = movieService.findMovieIsExist(name);
        if(null==movie){
            result = new Result("200","电影名不存在",null);
        }else {
            result = new Result("400","电影名已存在",movie.getId());
        }
        return result;
    }

    /**
     * 修改电影信息
     * @param movie
     * @return
     */
    @PostMapping("/admin/editMovie")
    @ResponseBody
    public Result editMovie(Movie movie){
        System.out.println(movie);
        Result result = null;
        Boolean flag = movieService.updateMovieById(movie);
        if(flag){
            result = new Result("200","修改成功",null);

        }else {
            result = new Result("500","修改失败",null);
        }
        return result;
    }
}
